<?php
// +----------------------------------------------------------------------
// | Function：测试类基础方法
// +----------------------------------------------------------------------
// | Copyright (c) 多享 2023
// +----------------------------------------------------------------------
// | Author: xiaochun <3204016924@qq.com>
// +----------------------------------------------------------------------
namespace yvhsse;

use \Exception;

class TestUnit
{


    /**
     * 调度方法
     * @param array $params 调用参数
     * @return false|string
     * @author: xiaochun <3204016924@qq.com>
     * @Date  ：2023-09-11 09:45
     */
    public static function dispatch(array $params)
    {
        try {
            if (!isset($params['class']) || !$params['class']) {
                throw new Exception('没有执行入口类');
            }
            if (!isset($params['method']) || !$params['method']) {
                throw new Exception('没有执行方法');
            }
            $class = $params['class'];
            if (!class_exists($class)) {
                throw new Exception('执行类不存在');
            }
            $method = $params['method'];
            if (!method_exists(new $class, $method)) {
                throw new Exception('执行方法不存在');
            }
            unset($params['class'], $params['method']);
            // 类如果不实例化则只能调用静态方法，实例化之后静态和非静态方法都可以调用
            $result = call_user_func_array([new $class, $method], $params);
        } catch (\Exception $e) {
            $result = ['code' => 0, 'msg' => ($e->getMessage() ?? '执行失败')];
        }
        return json_encode($result, 256);
    }
}